package me.xiaosiqi.web.controller.store;

import com.github.pagehelper.PageInfo;
import me.xiaosiqi.domain.store.Course;
import me.xiaosiqi.service.store.CourseService;
import me.xiaosiqi.service.store.impl.CourseServiceImpl;
import me.xiaosiqi.utils.BeanUtil;
import me.xiaosiqi.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaosiqi
 * @date 2021/6/3 18:01
 */

@WebServlet("/store/course")
public class CourseServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/javascript;charset=utf-8");
        String operation = req.getParameter("operation");
        System.out.println(operation);
        if ("list".equals(operation)){
            // uri:/store/course?operation=list
            list(req,resp);
        }else if ("delete".equals(operation)){
            // uri:/store/course?operation=delete
            delete(req,resp);
        }else if ("toAdd".equals(operation)){
            toAdd(req,resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            edit(req,resp);
        }



    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Course course = BeanUtil.fillBean(req, Course.class);
        courseService.update(course);
        resp.sendRedirect(req.getContextPath()+"/store/course?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Course course = courseService.findById(id);
        req.setAttribute("course",course);
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/store/course/update.jsp").forward(req,resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Course course  = BeanUtil.fillBean(req,Course.class,"yyyy-MM-dd");
        courseService.save(course);
        resp.sendRedirect(req.getContextPath()+"/store/course?operation=list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/store/course/add.jsp").forward(req,resp);

    }


    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Course course = new Course();
        course.setId(id);
        courseService.delete(course);
        resp.sendRedirect(req.getContextPath()+"/store/course?operation=list");

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 15;
       if (StringUtils.isNoneBlank(req.getParameter("page"))){
           page = Integer.parseInt(req.getParameter("page"));
       }
        if (StringUtils.isNoneBlank(req.getParameter("size"))){
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo pageInfo = courseService.findPage(page, size);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/store/course/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
